<?php
namespace Com\Pay;

//犇付QQ扫码支付

class QQBenfuPay{

    private $config = array();

    public function __construct($send_data){
        $this->config = $send_data;
    }

    /*生成支付连接*/
    public function getUrl($order_info){
        $merchantCode = $this->config['mch_id'];
        $returnUrl = $order_info['return_url'];
        $notifyUrl = S_URL . '/Api/NotifyCallback/BenfuNotify';

        $order_sn = $order_info['ordernumber'];
        $post_data = array(
            'p0_Version' => '1.0',
            'p1_MerchantNo' =>   $merchantCode,
            'p2_OrderNo'    =>     $order_sn,
            'p3_Amount'     =>  $order_info['paymoney'],
            "p4_Cur"        =>  1,
            "p5_ProductName"  =>  'ChangbaoQQ',
            "p8_ReturnUrl"  =>  $returnUrl,
            "p9_NotifyUrl"  =>  $notifyUrl,
            "q1_FrpCode"  =>  "QQ_NATIVE"
        );
        ksort($post_data);
        $post_data['hmac'] = MD5(implode('',$post_data).$this->config['mch_key']);
        $tar_url = 'http://www.wxhhht.top/trade/uniPayApi.action';
        $res = $this->curl_post($tar_url,$post_data);
         $res = json_decode($res,true);
        if($res['ra_Code'] == 100){
            $ret = $res['rc_Result'];
        }else{
            $ret = '';
        }
        return $ret;
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = parseData();
        $mykey 	= $this->config['mch_key'];
        $hmac = $data['hmac'];
        unset($data['hmac']);
        $data['ra_PayTime'] = urldecode($data['ra_PayTime']);
        $data['rb_DealTime'] = urldecode($data['rb_DealTime']);
        $sign = MD5(implode('',$data).$mykey);
        if($sign == $hmac){
            if($data['r6_Status'] == '100'){
                // 支付成功，签名验证成功
                return true;
            }else{
                // 失败
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }

    //查询订单号是否付款成功
    public function orderIsPay($order_info,$other = array()){
        $url = "http://www.wxhhht.top/trade/queryOrder.action";
        $postdata = array(
            'p1_MerchantNo'=>$this->config['merchantCode'],
            'p2_OrderNo'=>$order_info['order_number'],   //本站订单号
        );
        $postdata['hmac'] = md5(implode('', $postdata).$this->config['mch_key']);
        $resjson=$this->curl_post($url,$postdata);
        //print_r(json_decode($resjson,true)); exit();
        return json_decode($resjson,true);
    }


    public function curl_post($url, $data){
        $data = http_build_query($data);
        $opts = array(
            'http'=>array(
                'method'=>"POST",
                'timeout'=>60,
                'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
                    "Content-length:".mb_strlen($data,'utf-8')."\r\n" .
                    "Cookie: foo=bar\r\n" .
                    "\r\n",
                'content' => $data,
            )
        );
        $cxContext = stream_context_create($opts);
        $result = file_get_contents($url, false, $cxContext);
        return $result;
    }

    private function gbkToUtf8($arr){
        if(is_array($arr)){
            $res = array();
            foreach($arr as $k => $v){
                $res[$k] = $this->gbkToUtf8($v);
            }
            return $res;
        }else{
            return mb_convert_encoding ( $arr, "UTF-8", "GBK" );
        }
    }


}
